package com.blog.file;

import com.blog.Config;
import org.apache.http.HttpRequest;
import org.springframework.util.ResourceUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

/**
 * 2 * @Author: Tcj
 * 3 * @Date: 2021/1/20 20:31
 * 4
 */
public class HeaderUtil {
    /**
     * 处理头像,将用户最终上传的头像上传
     * @param fileName
     * @return
     */
    public static void getHeader(String fileName){
        {

            FileInputStream inputStream = null;
            FileOutputStream targetOutputStream = null;
            FileOutputStream outputStream = null;
            try {
                inputStream = new FileInputStream(new File( Config.updatePath + fileName));
                //将target目录也更新，否则需要重启服务器
                targetOutputStream = new FileOutputStream(Config.targetPath+fileName);

                outputStream = new FileOutputStream(new File(Config.readPath + fileName));
                byte[] buf = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buf)) > 0) {
                    outputStream.write(buf, 0, bytesRead);
                    targetOutputStream.write(buf,0,bytesRead);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {

                try {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    if (outputStream != null) {
                        outputStream.close();
                    }
                    if (targetOutputStream != null){
                        targetOutputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                File file = new File(Config.updatePath + fileName);
                file.delete();
            }
        }
    }
}
